
export const WebNetworkType = {
	"bluetooth" : 0,
	"cellular"	: 1,    // 2g, 3g, 4g, 5g
	"ethernet"	: 2,
	"wifi"		: 3,
	"wimax"		: 4,
	"none"		: 5,
	"other"		: 6,    
	"unknown"	: 7
}

export class WebNetworkState {
	constructor() {
		this.state_change_callback = null;
		 
		// Register for event changes:
		var connection = this.getConnection();
		if (connection) {
			connection.onchange = (e) => {
				if (this.state_change_callback) {
					this.state_change_callback(e);
				}
			};
		}
	}

	onChange = (cb) =>  {
		this.state_change_callback = cb;
	}

	getConnection = () => {
	  const nav = window.navigator;
	  if (!nav) return null;
	  // @ts-ignore
	  return nav.connection || nav.mozConnection || nav.webkitConnection;
	};

	getNetworkState = () => {
	  const connection = getConnection();
	  if (!connection) {
		return {
		  since: new Date(),
		};
	  }
	  const state = {
		since: new Date(),
		online: navigator.onLine,
		type: connection.type,
		effectiveType: connection.effectiveType,
		downlink: connection.downlink,
		downlinkMax: connection.downlinkMax,
		rtt: connection.rtt,
		saveData: connection.saveData,
	  };

	  console.table(state);

	  return state;
	};
}


//module.exports = {WebNetworkState:WebNetworkState, WebNetworkType:WebNetworkType};